package zhoug.demo.zui.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.MultiAutoCompleteTextView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import zhoug.demo.zui.R;
import zhoug.logging.Logger;
import zhoug.zui.ZUITopView;

/**
 * @Author: zhoug
 * @Date: 2023-11-01
 * @Description:
 */
public class TestAutoCompleteActivity extends AppCompatActivity {
    private static final String TAG = ">>>TestAutoCompleteActivit";
    private MyAppCompatAutoCompleteTextView mAutoCompleteTextView;
    private MyAppCompatMultiAutoCompleteTextView mMultiAutoCompleteTextView;
    private AutoCompleteAdapter<TestData> mAutoCompleteAdapter;
    private AutoCompleteAdapter<TestData> mMultiAutoCompleteAdapter;
    private ZUITopView mTopView;



    private static final List<TestData> DATA_SET;

    static {
        DATA_SET = new ArrayList<>();
        DATA_SET.add(new TestData("中国"));
        DATA_SET.add(new TestData("上海"));
        DATA_SET.add(new TestData("四川"));
        DATA_SET.add(new TestData("山东"));
        DATA_SET.add(new TestData("北京"));
        DATA_SET.add(new TestData("青岛"));
        DATA_SET.add(new TestData("南宁市"));
        DATA_SET.add(new TestData("南充市"));
        DATA_SET.add(new TestData("河北省"));
        DATA_SET.add(new TestData("乌鲁木齐"));
        DATA_SET.add(new TestData("重庆市渝中区"));
        DATA_SET.add(new TestData("重庆市渝北区"));
        DATA_SET.add(new TestData("南岸区"));
        DATA_SET.add(new TestData("西充县"));
        DATA_SET.add(new TestData("槐树镇"));
        DATA_SET.add(new TestData("中南乡"));
    }



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_auto_complete_activity);
        findViews();
        initView();
    }

    private void findViews() {
        mAutoCompleteTextView = findViewById(R.id.autoCompleteTextView);
        mTopView = findViewById(R.id.topView);
        mMultiAutoCompleteTextView = findViewById(R.id.multiAutoCompleteTextView);
    }

    private void initView() {
        mAutoCompleteAdapter = new AutoCompleteAdapter<>();
        mAutoCompleteAdapter.setFilter(new MyFilter(mAutoCompleteAdapter));
        mAutoCompleteTextView.setThreshold(1);
        mAutoCompleteTextView.setAdapter(mAutoCompleteAdapter);

        mMultiAutoCompleteAdapter = new AutoCompleteAdapter<>();
        mMultiAutoCompleteAdapter.setFilter(new MyFilter(mMultiAutoCompleteAdapter));
        mMultiAutoCompleteTextView.setThreshold(1);
        mMultiAutoCompleteTextView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
        mMultiAutoCompleteTextView.setAdapter(mMultiAutoCompleteAdapter);
    }


    private class MyFilter<T> extends Filter {
        private AutoCompleteAdapter<T> adapter;

        public MyFilter(AutoCompleteAdapter<T> adapter) {
            this.adapter = adapter;
        }

        /**
         * 子线程
         *
         * @param constraint
         * @return
         */
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            Logger.d(TAG, "performFiltering:constraint:" + constraint);
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            FilterResults filterResults = new FilterResults();
            if (constraint == null || constraint.length() == 0) {
                filterResults.count = DATA_SET.size();
                filterResults.values = new ArrayList<>(DATA_SET);
            } else {
                String filterText = constraint.toString().trim();
                List<TestData> testData = new ArrayList<>();
                for (TestData item : DATA_SET) {
                    if (item.getName() != null && item.getName().contains(filterText)) {
                        testData.add(item);
                    }
                }
                filterResults.count = testData.size();
                filterResults.values = testData;
            }
            return filterResults;
        }

        /**
         * 主线程
         *
         * @param constraint
         * @param results
         */
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            if (results != null) {
                Logger.d(TAG, "publishResults:count:" + results.count);
                Logger.d(TAG, "publishResults:values:" + results.values);
                List<T> testData = (List<T>) results.values;
                adapter.setData(testData);
                adapter.notifyDataSetChanged();
            } else {
                adapter.setData(new ArrayList<>());
                adapter.notifyDataSetChanged();
            }
        }
    }

    private static class AutoCompleteAdapter<T> extends BaseAdapter implements Filterable {
        private List<T> mData;
        private Filter mFilter;

        public void setData(List<T> data) {
            this.mData = data;
        }

        public List<T> getData() {
            return mData;
        }

        public void setFilter(Filter filter) {
            this.mFilter = filter;
        }

        @Override
        public int getCount() {
            return mData != null ? mData.size() : 0;
        }


        @Override
        public T getItem(int position) {
            if (mData != null && position < mData.size()) {
                return mData.get(position);
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.test_list_item, parent, false);
            }
            TextView tvName = convertView.findViewById(R.id.tv_text);
            Object item = getItem(position);
            if (item != null) {
                tvName.setText(item.toString());
            }
            return convertView;
        }


        @Override
        public Filter getFilter() {
            return mFilter;
        }

    }


    private static class TestData {
        private String id;
        private String name;

        public TestData(String name) {
            id = UUID.randomUUID().toString();
            this.name = name;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        @Override
        public String toString() {
            return name;
        }
    }
}
